package com.course.server.service.discusspost;

import com.course.server.domain.Member;
import com.course.server.domain.discusspost.Comment;
import com.course.server.domain.discusspost.DiscussPost;
import com.course.server.dto.PageDto;
import com.course.server.dto.discusspost.CommentDto;
import com.course.server.mapper.my.CommentMapper;
import com.course.server.mapper.my.MyDiscussPostMapper;
import com.course.server.service.MemberService;
import com.course.server.util.CommunityConstant;
import com.course.server.util.SensitiveFilter;
import com.course.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentService implements CommunityConstant {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private MyDiscussPostMapper myDiscussPostMapper;

    @Autowired
    private MemberService memberService;

    public List<Comment> findCommentsByEntity(String entityType, String entityId, int offset, int limit) {
        return commentMapper.selectCommentsByEntity(entityType, entityId, offset, limit);
    }

    public int findCommentCount(String entityType, String entityId) {
        return commentMapper.selectCountByEntity(entityType, entityId);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public int addComment(Comment comment) {
        if (comment == null) {
            throw new IllegalArgumentException("参数不能为空!");
        }

        // 添加评论
//        comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
        comment.setContent(comment.getContent());
        comment.setContent(sensitiveFilter.filter(comment.getContent()));
        comment.setId(UuidUtil.getShortUuid());
        int rows = commentMapper.insertComment(comment);

        // 更新帖子评论数量
        if (comment.getEntityType().equals(ENTITY_TYPE_POST) ) {
            int count = commentMapper.selectCountByEntity(comment.getEntityType(), comment.getEntityId());
            discussPostService.updateCommentCount(comment.getEntityId(), count);
        }

        return rows;
    }

    public Comment findCommentById(String id) {
        return commentMapper.selectCommentById(id);
    }

    public void findUserComments(PageDto pageDto, String userId) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<Comment> commentList = commentMapper.selectCommentsByUser(userId);
        PageInfo<Comment> pageInfo = new PageInfo<>(commentList);
        pageDto.setTotal(pageInfo.getTotal());
        List<CommentDto> commentDtoList=new ArrayList<>();
        for (Comment comment : commentList) {
            CommentDto commentDto = new CommentDto();
            commentDto.setComment(comment);
            DiscussPost discussPost = myDiscussPostMapper.selectDiscussPostById(comment.getEntityId());
            commentDto.setDiscussPost(discussPost);
            Member member = memberService.findUserById(discussPost.getUserId());
            commentDto.setMember(member);
            commentDtoList.add(commentDto);
        }
        pageDto.setList(commentDtoList);
    }

}
